package app.dinus.com.example;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import app.dinus.com.loadingdrawable.LoadingView;
import app.dinus.com.loadingdrawable.render.scenery.ElectricFanLoadingRenderer;

public class SceneryActivity extends AppCompatActivity {

    private ValueAnimator mRenderAnimator;
    private ElectricFanLoadingRenderer mLoadingRenderer;

    private LoadingView loadingView;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, SceneryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenery);


        loadingView = findViewById(R.id.electric_fan_view);
        loadingView.setOnClickListener(mClickedListener);

        mLoadingRenderer = loadingView.render();
        mLoadingRenderer.setText("START");
    }

    private View.OnClickListener mClickedListener = new View.OnClickListener() {
        private float animValue = 0.0F;

        @Override
        public void onClick(View v) {
            animValue = 0F;
            mRunnable.run();
            loadingView.startAnimation();
        }

        private Runnable mRunnable = new Runnable() {
            private Handler mHandle = new Handler();

            @Override
            public void run() {
                mLoadingRenderer.setProgress(animValue);
                mLoadingRenderer.setText(animValue >= 1 ? "FINISH" : "RUNNING...");

                animValue += 0.1F;

                if (animValue <= 1.05) {
                    mHandle.postDelayed(this, 500);

                } else {
                    animValue = 0F;

                    mHandle.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            mLoadingRenderer.setText("START");
                            mLoadingRenderer.reset();
                            mLoadingRenderer.invalidate();
                        }
                    }, 2000);
                }
            }
        };
    };
}
